package com.shop.shoppingmall.service.impl;

import com.shop.shoppingmall.dao.ShBannerDao;
import com.shop.shoppingmall.dao.ShMerchantDao;
import com.shop.shoppingmall.dao.ShProductDao;
import com.shop.shoppingmall.entity.ShBanner;
import com.shop.shoppingmall.entity.ShMerchant;
import com.shop.shoppingmall.entity.ShProduct;
import com.shop.shoppingmall.service.ShBannerService;
import com.shop.shoppingmall.util.UUIDGenerator;
import com.shop.shoppingmall.util.PhotoUtil;
import com.shop.shoppingmall.util.api.vo.Result;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

/**
 * (ShBanner)表服务实现类
 *
 * @author makejava
 * @since 2020-09-10 09:18:22
 */
@Service("shBannerService")
@Transactional
public class ShBannerServiceImpl implements ShBannerService {
    @Resource
    private ShBannerDao shBannerDao;
    @Resource
    private ShProductDao shProductDao;
    @Resource
    private ShMerchantDao shMerchantDao;

    /**
     * 通过ID查询单条数据
     *
     * @param id 主键
     * @return 实例对象
     */
    @Override
    public ShBanner queryById(String id) {
        return this.shBannerDao.queryById(id);
    }

    /**
     * 查询多条数据
     *
     * @param offset 查询起始位置
     * @param limit  查询条数
     * @return 对象列表
     */
    @Override
    public List<ShBanner> queryAllByLimit(int offset, int limit) {
        return this.shBannerDao.queryAllByLimit(offset, limit);
    }

    /**
     * 新增数据
     *
     * @param shBanner 实例对象
     * @return 实例对象
     */
    @Override
    public Result insert(ShBanner shBanner) {
        shBanner.setId(UUIDGenerator.generate());
       shBannerDao.insert(shBanner);
        return Result.ok("添加成功");
    }

    /**
     * 修改数据
     *
     * @param shBanner 实例对象
     * @return 实例对象
     */
    @Override
    public Result update(ShBanner shBanner) {
      shBannerDao.update(shBanner);
        return Result.ok("修改成功!");
    }

    /**
     * 通过主键删除数据
     *
     * @param id 主键
     * @return 是否成功
     */
    @Override
    public Result deleteById(String id) {
        shBannerDao.deleteById(id) ;
        return Result.ok("删除成功!");
    }

    /**
     * 查询出所有轮播图信息
     * @return
     */
    @Override
    public Result<Object> selectBannerAll() {
        List<ShBanner> shBanners = shBannerDao.selectBannerAll();
        if(shBanners == null){
            return Result.ok("暂时没有图片信息");
        }
        return Result.ok(shBanners);
    }

    /**
     * 点击轮播图跳转商品商铺的详情
     * @param id
     * @param bannerType
     * @return
     */
    @Override
    public Result selectBannerProductAndShop(String id,String bannerType) {
        if(bannerType.equals("1")){
            ShProduct shProduct = shProductDao.queryById(id);
            if(shProduct == null){
                return Result.ok("暂时没有该商品的详情");
            }
            return Result.ok(shProduct);
        }else{
            ShMerchant shMerchant = shMerchantDao.queryById(id);
            if(shMerchant == null){
                return Result.ok("暂时没有该商铺的信息");
            }
            return Result.ok(shMerchant);
        }
    }

}